<?php 
/**
 * Supplier controller is write by Norman 
 * @author Norman
 *
 */
class SupplierController extends Zend_Controller_Action
{

    public function init(){
    /**
	 *
	 *
	 */    
	
	}

	/**
	 * This is the landing page of the supplier controller 
	 * it shows all the supplier 
	 */
    public function indexAction(){
	
		echo "<h2>Supplier Names </h2>";
		$suppliers = new Model_DbTable_Suppliers();
		$rows = $suppliers->listAllSuppliers();
		
		$this->view->supplierlist = $rows;
		

    
	}
	/**
	 * add supplier into supplier db
	 */
	public function addSupplierAction(){
		
		$suppliers = new Model_DbTable_Suppliers();
		
		if($this->getRequest()->isPost())
		{
				//saving the data , echo "saved"
			$suppliers->addSupplier($_POST['name_supplier'],$_POST['short_name_supplier'],$_POST['location_supplier']);
			echo "DONE";
		}
		
		
	}
	
	/**
	 * This function is no longer in use
	 * this is not in good use 
	 * this list all the sampled provided by china
	 */
	public function listSampleAction(){
	 $samples = new Model_DbTable_Sampleproducts();
	 $this->view->samplelist = $samples->listUndecideSamples();	
	}
	
	/**
	 * this page is for create new sample
	 * this is no longer in good use
	 */
	public function newSampleAction(){
		
	}	
	
	/**
	 * this is the actual page to save the sample in sample db
	 */
	public function saveNewSampleStepAAction(){
		
		$staffName = $_POST['staff_name'];
		$idSupplier = $_POST['id_supplier'];
		$chineseName = $_POST['chinese_name'];
		$barcodeSupplier = $_POST['barcode_supplier'];
		
		
		$samples = new Model_DbTable_Sampleproducts();
		$barcodeNoColor = $samples->createNewBarcode($idSupplier);
		$arrBarcode = array();
		$arrColor = $_POST['color_code'];
		
		foreach($arrColor as $key => $value){
			$newbarCode = "SAP-".$barcodeNoColor."-".$value;
			$arrBarcode[] = $newbarCode;

			$samples->addSample($newbarCode, $chineseName, $idSupplier, $staffName, $barcodeSupplier);
		
		}
		
		
		
		$this->view->barcodenocolor = $barcodeNoColor;
		$this->view->arrBarcode = $arrBarcode;
		$this->view->colorcode = $arrColor;
		
		
		
		
		
	}
	/**
	 * This funcion not start yet
	 */
	public function saveNewSampleStepBAction(){
		
	}	
	
	/**
	 * this funcon is for upload the photo file
	 */
	public function uploadPhotosAction(){
		
	}
	/**
	 * this function not start yet
	 */
	public function updateBuyingPriceAction(){
		
	}
	/**
	 * this funcon is to resize the sample picture to thumb
	 */
	public function savethumbsAction(){
		
		echo $barCode = $this->_getParam('id');
		
		$samples = new  Model_DbTable_Sampleproducts();
		$samples->updateSampleThumb($barCode);
		
	}
	/**
	 * this is the fucntion for take the sample picture 
	 */
	public function saveImgsAction(){
		
		$barCode = $this->_getParam('id');
		$thumbdir = getcwd()."/im/thumbs_sample/";
		$zipdir = getcwd()."/im/zip_sample/";
		$thumbFileName = $barCode.".jpg";
		$zipFileName = $barCode.".zip";
		
		$samples = new  Model_DbTable_Sampleproducts();
		
		
		
		if($this->getRequest()->isPost()){
			if(isset($_POST['is_thumb'])){
				if(!move_uploaded_file($_FILES['thumb_img']['tmp_name'],$thumbdir.$thumbFileName)){
					echo "Somthing is wrong";
				}
				else{
					$samples->updateSampleThumb($barCode);
					echo "Thumb File Upload Done, Close";
				}
				
			}	
			if(isset($_POST['is_zip'])){
				if(!move_uploaded_file($_FILES['img_zip']['tmp_name'],$zipdir.$zipFileName)){
					echo "Somthing is wrong";
				}
				else{
					$samples->updateSampleZip($barCode);
					echo "Zip File Upload Done, Close";
					
				}			
			
			}			
			
		}
		
	}

	/**
	 * This is once off funcion to import supplier from guan jia po excel file 
	 */	
	public function importSupplierAction(){
		
		$sp = new Model_DbTable_Suppliers();
		$fl = fopen("info.csv","r");
		while(($lineData = fgets($fl))!=false){
			//d($lineData);
			$str =iconv( "UTF-8", "UTF-8",$lineData);
			$arr = explode(",", $str);		 
			//var_dump($arr);
		
			//$sp->addSupplier($arr[1], $arr[0],'CHINA');
		}
	}
}
?>